/**
 * Malsha Sewwandi 2014-06-27
 */

Receipt = new Object();

receiptid = null;

var receipt_store = Ext.create('Ext.data.Store', {
	fields : [ 'id', 'bankId', 'amount', 'bankAccount', 'bankName', 'chequeDate', 'chequeNo', 'confDate', 'confUserId', 'customerId', 'customerType', 'instruction', 'paymentType',
			'purpose', 'receiptNo', 'reqDate', 'reqUserId', 'status', 'customerCode', 'customerName', 'fundid', 'fundName', 'fundBank', 'fundAccount', 'chequeBank' ]
});

var rec_customer_accounts = Ext.create('Ext.data.Store', {
	fields : [ 'accountNo', 'branch', 'nameOfBank', 'customer', 'bankid' ]
});

Receipt.getCustomerDetails = function() {
	return Ext.create('Ext.form.FieldSet', {
		title : 'Customer Details',
		defaults : {
			anchor : '100%'
		},
		items : [ {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'textfield',
				fieldLabel : 'Customer Code',
				readOnly : true,
				flex : 1.25,
				name : 'customerCode',
				padding : '0 15 0 0'
			}, {
				xtype : 'textfield',
				fieldLabel : 'Customer Name',
				readOnly : true,
				name : 'customerName',
				flex : 2
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			fieldLabel : 'Bank Account <span style="color:red">*</span>',
			items : [ {
				xtype : 'combo',
				flex : 1,
				store : rec_customer_accounts,
				valueField : 'bankid',
				displayField : 'accountNo',
				queryMode : 'local',
				allowBlank : false,
				id : 'recbank_account',
				msgTarget : 'under',
				name : 'bankId',
				padding : '0 8 0 0',
				listeners : {
					change : function(combo, record, index) {
						if (combo.getValue() == "" || combo.getValue() == "&nbsp;" || combo.getValue == 0)
							combo.setValue(null);
					}
				},
				listConfig : {
					listeners : {
						itemclick : function(list, record) {
							if (record.get('nameOfBank') != null) {
								Ext.getCmp('bankName').setValue(record.get('nameOfBank'));
							}
						}
					}
				}
			}, {
				xtype : 'textfield',
				emptyText : 'Name of Bank',
				flex : 2,
				id : 'bankName',
				name : 'bankName',
				readOnly : true,
				allowBlank : false,
			} ]
		} ]
	});
};

Receipt.getFundAccount = function() {
	return Ext.create('Ext.form.FieldSet', {
		title : 'Fund Account Details',
		defaults : {
			anchor : '100%'
		},
		items : [ {
			xtype : 'textfield',
			fieldLabel : 'Fund',
			flex : 2,
			name : 'fundName',
			readOnly : true,
			allowBlank : false,
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			fieldLabel : 'Bank Account',
			items : [ {
				xtype : 'textfield',
				emptyText : 'Account Number',
				flex : 1,
				name : 'fundAccount',
				allowBlank : false,
				readOnly : true,
				padding : '0 5 0 0'
			}, {
				xtype : 'textfield',
				emptyText : 'Name of Bank',
				flex : 2,
				name : 'fundBank',
				readOnly : true,
				allowBlank : false,
			} ]
		}, {
			xtype : 'textfield',
			fieldLabel : 'Purpose',
			name : 'purpose',
		} ]
	});
};

Receipt.getCenterPanel = function() {

	rec_customer_accounts.removeAll();
	var customer = Receipt.getCustomerDetails();
	var fund = Receipt.getFundAccount();
	return Ext.create('Ext.form.Panel', {
		title : 'Receipt Confirmation',
		bodyPadding : '20',
		autoScroll : true,
		defaults : {
			anchor : '65%'
		},
		id : 'receipt_form',
		frame : true,
		items : [ {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'triggerfield',
				fieldLabel : 'Receipt No.',
				triggerCls : 'x-form-search-trigger',
				flex : 1,
				name : 'receiptNo',
				padding : '0 15 0 0',
				editable : false,
				listeners : {
					specialkey : function(field, e) {
						if (e.getKey() == e.ENTER) {

						}
					}
				},
				onTriggerClick : function() {
					var window = null;
					Ext.Ajax.request({
						url : 'getReceipt.action',
						success : function(response) {
							var obj = Ext.decode(response.responseText);
							receipt_store.removeAll();
							receipt_store.loadRawData(obj.receiptDtos);
						}
					});
					if (!window) {
						window = Ext.create('Ext.window.Window', {
							title : 'Receipt',
							height : 330,
							frame : true,
							width : 500,
							constrain : true,
							resizable : false,
							modal : true,
							layout : {
								type : 'fit'
							},
							items : [ {
								xtype : 'gridpanel',
								defaults : {
									anchor : '100%'
								},
								store : receipt_store,
								columns : [ {
									text : 'Code',
									dataIndex : 'receiptNo',
									flex : 1
								}, {
									text : 'Name',
									dataIndex : 'customerName',
									flex : 1
								} ],
								listeners : {
									itemdblclick : function(dv, record, item, index, e) {
										var data = record.data;
										Receipt.setFormFields(data, record);
										window.hide();
									}
								}
							} ]
						});
					}
					;
					if (window.isVisible()) {
						window.hide();
					} else {
						window.show();
					}
				}
			}, {
				xtype : 'datefield',
				fieldLabel : 'Date',
				editable : false,
				value : new Date(),
				flex : 1
			} ]
		}, {
			xtype : 'textfield',
			fieldLabel : 'Amount',
			anchor : '29.5%',
			name : 'amount',
			fieldStyle : "text-align:right;",
			maskRe : /[\d\,.]/,
			readOnly : true,
			listeners : {
				blur : function() {
					var totaluni = this.value.replace(/,/g, '');
					this.setRawValue(Ext.util.Format.number(totaluni, '0,000.0000'));
				},
				render : function() {
					this.setRawValue(Ext.util.Format.number(this.value, '0,000.0000'));
				},
				change : function() {
					var totaluni = this.value.replace(/,/g, '');
					this.setRawValue(Ext.util.Format.number(totaluni, '0,000.0000'));
				},
			}
		}, {
			xtype : 'radiogroup',
			fieldLabel : 'Payment Type',
			columns : 4,
			anchor : '50%',
			labelWidth : 110,
			vertical : true,
			items : [ {
				boxLabel : 'RTGS',
				name : 'paymentType',
				inputValue : 'R',
				readOnly : true,
				checked : true
			}, {
				boxLabel : 'SLIP',
				name : 'paymentType',
				inputValue : 'S',
				readOnly : true,
			}, {
				boxLabel : 'CASH',
				name : 'paymentType',
				inputValue : 'T',
				readOnly : true,
			}, {
				boxLabel : 'CHEQUE',
				name : 'paymentType',
				inputValue : 'C',
				readOnly : true,
				listeners : {
					change : function(field, newValue, oldValue) {
						if (newValue) {
							Ext.getCmp('chequeFields').setDisabled(false);
						} else {
							Ext.getCmp('chequeFields').setDisabled(true);
						}
					}

				}
			} ]
		}, customer, fund, {
			xtype : 'fieldset',
			title : 'Cheque',
			id : 'chequeFields',
			disabled : true,
			defaults : {
				anchor : '100%'
			},
			items : [ {
				xtype : 'fieldcontainer',
				layout : 'hbox',
				items : [ {
					xtype : 'textfield',
					fieldLabel : 'Cheque No.',
					flex : 1,
					name : 'chequeNo',
					padding : '0 5 0 0'
				}, {
					xtype : 'datefield',
					fieldLabel : 'Cheque Date',
					flex : 1,
					format : 'd/m/Y',
					name : 'chequeDate'
				} ]
			}, {
				xtype : 'fieldcontainer',
				layout : 'hbox',
				items : [ {
					xtype : 'textfield',
					fieldLabel : 'Bank',
					flex : 1,
					name : 'chequeBank',
					padding : '0 5 0 0'
				}, {
					xtype : 'textfield',
					flex : 1,
					fieldLabel : 'Instruction'
				} ]
			} ]
		} ],
		buttonAlign : 'center',
		buttons : [ {
			text : 'Cancel',
			handler : function() {
				App.Core.removeScreen();
			}
		}, {
			text : 'Reset',
			handler : function() {
				dealTypeValue = null;
				dealStore.removeAll();
				this.up('form').getForm().reset();
			}
		}, {
			text : 'Confirm',
			handler : function() {
				if (receiptid != null) {
					var form = this.up('form').getForm();
					if (form.isValid()) {
						form.url = 'confirmReceipt.action';
						form.submit({
							params : {
								"receiptid" : receiptid,
							},
							success : function(form, action) {
								form.reset();
								receiptid = null;
								Ext.Msg.alert('Success', 'Saved Successfully');
							},
							failure : function(form, action) {
								Ext.Msg.alert('Failed', action.result.resulteMsg);
							}
						});
					}
				}
			}
		}, {
			text : 'Reject',
			id : 'reject',
			handler : function() {
				var form = this.up('form').getForm();
				if (receiptid != null) {
					if (form.isValid()) {
						form.submit({
							url : 'rejectReceipt.action',
							method : 'GET',
							params : {
								"receiptid" : receiptid
							},
							success : function(form, action) {
								form.reset();
								investmentId = null;
								Ext.Msg.alert('Success', 'Receipt Reject');
							},
							failure : function(form, action) {
								Ext.Msg.alert('Failed', action.result.resulteMsg);
							}
						});
					}
				} else {
					Ext.Msg.alert('Info', 'Select the Receipt');
				}
			}
		}, {
			text : 'Print',
			handler : function() {
				if (receiptid != null) {
					var param = {
						'receiptid' : receiptid
					};
					Report.OpenWindowWithPost('ReportServlet/InvestmentReceipt', "width=1000, height=600, left=100, top=100,resizable=yes, scrollbars=yes", "NewFile", param);
				} else {
					Ext.Msg.alert('Info', 'Select the Receipt');
				}
			}
		} ]
	});
};

Receipt.setFormFields = function(data, record) {
	var formPanl = Ext.getCmp('receipt_form');
	receiptid = data.id;

	Ext.Ajax.request({
		url : 'getBankAccountByCustomerType.action',
		params : {
			"customer_id" : data.customerId,
			"customerType" : data.customerType,
		},
		success : function(response) {
			var obj = Ext.decode(response.responseText);
			Ext.getCmp('recbank_account').clearValue();
			rec_customer_accounts.removeAll();
			rec_customer_accounts.loadRawData(obj.accountDtos);
		}
	});

	if (data.paymentType == 'T' || data.paymentType == 'C') {
		Ext.getCmp('recbank_account').disable();
		Ext.getCmp('bankName').disable();
	} else {
		Ext.getCmp('recbank_account').enable();
		Ext.getCmp('bankName').enable();
	}
	formPanl.getForm().loadRecord(record);

};